export default defineStore({
    id: 'app',
    state: () => {
        return {
            systemInfo: {},
            menuButtonInfo: {},
            appId: '',
            locationInfo:{
                latitude: "",
                longitude: "",
            },
            splashLoadNumber:0
        } as {
            systemInfo: UniApp.GetSystemInfoResult;
            menuButtonInfo: UniApp.GetMenuButtonBoundingClientRectRes;
            appId?: string,
            locationInfo:{
                latitude: number | string,
                longitude: number | string,
            },
            splashLoadNumber: number
        };
    },
    getters: {
        getSystemInfo(): UniApp.GetSystemInfoResult {
            return this.systemInfo
        },
        getMenuButtonInfo(): UniApp.GetMenuButtonBoundingClientRectRes {
            return this.menuButtonInfo
        },
        getAppId(): string {
            return this.appId as string;
        },
        getSplashLoadNumber(): number {
            return this.splashLoadNumber;
        }
    },
    actions: {
        setSystemInfo() {
            if (Object.keys(this.systemInfo).length) return
            const systemInfo = uni.getSystemInfoSync();
            Object.assign(this.systemInfo, systemInfo);
            this.systemInfo = systemInfo
        },
        setMenuButtonInfo() {
            // #ifdef MP-WEIXIN
            if (Object.keys(this.menuButtonInfo).length) return
            const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
            Object.assign(this.menuButtonInfo, menuButtonInfo);
            this.menuButtonInfo = menuButtonInfo
            // #endif
        },
        setAppId(value?: string) {
            // #ifndef MP-WEIXIN
            this.appId = value
            // #endif
            // #ifdef MP-WEIXIN
            let obj = uni.getAccountInfoSync()
            this.appId = obj.miniProgram.appId
            // #endif
        },
        setLocationInfo() {
            uni.getLocation({
                type: 'gcj02',
                success: res => {
                    console.log(res,'res')
                    let { latitude, longitude } = res;
                    this.locationInfo.latitude = latitude;
                    this.locationInfo.longitude = longitude;
                    uni.setStorageSync("locationInfo",this.locationInfo);
                },
                fail:(err) => {
                    console.log(err,'err-getLocationInfo');
                }
            })
        },
        setSplashLoadNumber() {
            this.splashLoadNumber += 1;
        }
    }
});
